import requests
import decryptDefine as md
from bs4 import BeautifulSoup
from netEaseEncode import *
import json


# 开始请求
r = requests.session()
# 请求头
headers = {
    'origin': 'https://music.163.com',
    'referer': 'https://music.163.com/',
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.81",
    "Cookie": "_ntes_nnid=a52e7a7dc68aec1a8d5832401a8db591,1684237179682; _ntes_nuid=a52e7a7dc68aec1a8d5832401a8db591; WEVNSM=1.0.0; WNMCID=zierib.1684237179928.01.0; NMTID=00O1bgDLAlPa5rDSUDjvIZ9SE3a9M4AAAGIJ3P28w; __snaker__id=O99BWWrznL19aanE; YD00000558929251%3AWM_TID=MMUBpc2DHPFEBUEVRRPRlVlvPjEVNPv8; ntes_kaola_ad=1; sDeviceId=YD-Nm82uPIHd2BBA1ABFRLAKGOAm/Zy82Ry; __bid_n=1887b1a6dd9da601504207; FPTOKEN=le4+96UjnPItM+hFpa7KlJsIl/GTSkDpfmZyh85D/kZq30ZoycbT2lN8c6aRwItSl+gMmKJrdluZVDbwvSJY2f9H+53s/E+oAaumNIHkOiNNil6jL54eBGsZm5mePkqQfR1cbFXkfY+svG7jA7gT36fNuLWCQdzvbps9ycFRD8FhFKAQ/RLDK93ntiAGihSr0IeGZ0e4ysjnP6mPgn41dCo4aUT3R5nmoRNsLwHRGrQ+JfG0H3nTx7xxCOmNhehPd5y6tzWq4jCSh6d/NTDCKodojwhiWn6rKElCRZc+ZYs8MHvnWiK6kIJaiKtnLuRBGf4SCXOTnJJRAvSef2jsW7ZG3iB+P3v0YgtIiH4OneYTxuH9agi9UMitfk7VBf7IwbCaZq8luS14x2OvfTpB6w==|SqtmhzdHmFLWdoCi3BGKhHT3xlvC8r1xh6RdIg/w9zI=|10|9b552f9985d5e9370c5f74a513f785fa; _iuqxldmzr_=32; NTES_P_UTID=ZUbdneVvJVDIZEhwgvf2SM2Xlzi9Mk9B|1688785447; P_INFO=roysxlife@163.com|1688785447|0|mail163|00&99|CN&1688522490&carddav#gud&null#10#0#0|&0|mailmaster_android|roysxlife@163.com; YD00000558929251%3AWM_NI=Zf9BDkB09%2F57e2dJ88M36eL6ghN2uIu%2FoYSAXkhC0TisvHSqguEDIgRGC0DdptUAiNlqg7rG9A%2FZ%2FaTt3xlLEmGy5UWWIPTnFuJELsYwQuvejHtmjrsLxl1N%2B0pZqxUnU08%3D; YD00000558929251%3AWM_NIKE=9ca17ae2e6ffcda170e2e6eed0ee4893988fa9ae7db29e8fa7c84b869e8aadd464aa9da2a4aa5d8894bcb1c62af0fea7c3b92a9abaf7a3c76182f0f7d0b83da7eeacabbb348fedfa86cf3ea5b882d3f843b7f1a1bad24b9cb5a8a4b360abadbaa9d27ba5ab8ba5cc6d92998dd1c770b4ac9ba2ec46b0afa9d8f14dfb9fe1bbae6b98acfd99ef4887b3899ab85f958987b1d76686ab86a6cf799ba99f96f56faa98bc8ee97ab194b88db446e9f0c0bbf47497b29bd1bb37e2a3; MUSIC_U=00DB660AC2402C45DC3192457E05ED0342C661FC4EE2DC878C91A9B01ED61517B5F402D4340713761FF6B83CF6B5F3FD9E60E58C37C9A78824675BEF9F57D730E1DDBA9922AA14320B984708B882BB0905CB203FA8DC961065EEFA84B630F173758BD06DFB2D0714EB07061E3730098FAF0002F2C5447426286ED3EB6FF98429E783251C4D3EF62C8226C95645DC192785B590D8D69C3C10E509CEACC38301CB893C4C91EB8DAE3B31D46D98A90EF90394F7DA3489045BAC49A56572CD1AC147086554AA36942B6B4E5A94665696F48DB693E4DE2EF37263A1736A2A9757BAF1DD02B1CBD07A0A001D39F4CF25901DE326FC66EB9E32C9F35FBF31C0FDAA29EC6C29D3D3DC1459C0FCF7AA13A901BA4B0C6FA90742BAD85D9E537CE599CF1394E879DC82EE14DB267B5364F3257F8E844B7ABAB70925A67F390164686127A0052B29A651CE3E9BA40335321F71F1AA11E559D78F5B0E34B291E70613EBFA673B60B4186510110BD1A513307E7967D19D4E; WM_TID=voxRPE1eb%2BZFRABFFQbByYJ%2F4bpzPgy7; ntes_utid=tid._.03dteUQSXF9FR0BQRBLRyINqof9y06PG._.0; __csrf=0974b66c30c6f4bc4103a91e0d4a44f2; __csrf=0974b66c30c6f4bc4103a91e0d4a44f2; JSESSIONID-WYYY=ZcCRy%2BuXYhqWB4eTOZ%5C%2B2b4X66kdtBxKavJvj94ntzjd76fb%2FXIiOjPq8bQMxAA%5CcpXYdyuXXlUdE5Fz%5CpdjBeP8f1DztSxbvnvFGCDod8y%5CSwQoVG2DZuFzVGrPjyevvsbjxXkNA5QRWRZaA2JT3lbuCEkD1sdvsJp0z3u1XZ%2BaXo8j%3A1698152647622; WM_NI=0gYSArWb7IrMN06714Z90cyHm8ciO1AifMjOJpf0FdckVWqPAD1uzY%2FHCE%2FFGV3Vj1NPQpYhKLb0fnM7oWJgka7QskG8JDt2iijG611rqoGmoqZNWx4expNmW5FkhvQfb3E%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6eeb3cb3a96949ba9d2688deb8ba2d55a839b9e83c162abb498d4d36b9b9afed4d82af0fea7c3b92af4b997b4c47a95bef9aaf1478ef0fad8b4409ae70084fc44afe8ad8ace3b83b7aaaee9458ab0aad1c570a1b59ab8f45c839cff96f953ed988da6ae4e858afdb7e93b8e909f91dc648994aed1e5668fe9a1b8c96da9bff9b2f26185e886a5f73aabb7a6a8b53bf3aebf90c667adaf96afd674f8bbb891cd6795eb8a8ceb7e93baaed4c837e2a3"
}

search_url="https://music.163.com/weapi/cloudsearch/get/web"

def search_song(songname):
    offset = 0
    limit = 30
    stype = 1

    data = {
        's': songname,
        'offset': str(offset),
        'limit': str(limit),
        'type': str(stype)
    }

    data = encrypted_request(data)
    rep = r.post(search_url, data=data, headers=headers)
    print(rep)
    keyjs = json.loads(rep.text)
    songId = keyjs["result"]["songs"][0]["id"]
    print(songId)
    return songId



def download(songid,songname):
    
    # 解密
    do = md.Encrypyed()
    # 请求参数
    data = do.d(md.getKey(songid))
    # 请求url
    url = 'https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token='
    html = r.post(url, data=data, headers=headers)
    song_url = html.json()['data'][0]['url']
    # print(song_url)
    response= r.get(song_url, headers=headers)
    with open("C:/Users/roysx/Pictures/Desktop/"+songname+".mp3", 'wb') as f:
        f.write(response.content)
    return "C:/Users/roysx/Pictures/Desktop/"+songname+".mp3"

if __name__=='__main__':
    songname="方圆几里"
    download(str(search_song(songname)),songname)